package com.cz.video.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.common.constant.CacheNames;
import com.cz.video.common.constant.CommonConstants;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.utils.StringUtils;
import com.cz.video.website.domain.WebsiteMovie;
import com.cz.video.website.domain.bo.WebsiteMovieBo;
import com.cz.video.website.domain.vo.WebsiteMovieVo;
import com.cz.video.website.mapper.WebsiteMovieMapper;
import com.cz.video.website.service.IWebsiteMovieService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 电影表 服务实现类
 * </p>
 *
 * @author 洪圆胜
 * @since 2023-10-15
 */
@Service
@RequiredArgsConstructor
public class WebsiteMovieServiceImpl extends ServiceImpl<WebsiteMovieMapper, WebsiteMovie> implements IWebsiteMovieService {

    private final WebsiteMovieMapper websiteMovieMapper;
    /**
     * 新增电影
     *
     * @Param websiteMovie 电影信息
     * @return 结果
     */

    @Override
    public int addMovie(WebsiteMovieBo websiteMovie) {
        int rows = websiteMovieMapper.insert(websiteMovie);
        return rows;
    }

    @Override
    @Cacheable(cacheNames = CacheNames.WEBSITE_MOVIE, key = "#id")
    public WebsiteMovieVo get(Long id) {
        return websiteMovieMapper.selectDetailInfo(id);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.WEBSITE_MOVIE, key = "#id")
    public int deleteById(Long id) {
        return websiteMovieMapper.deleteById(id);
    }

    @Override
    public TableDataInfo<WebsiteMovieVo> page(WebsiteMovieBo websiteMovie, PageQuery pageQuery) {
        Page<WebsiteMovieVo> page=websiteMovieMapper.selectPageList(buildQueryWrapper(websiteMovie), pageQuery);
        return TableDataInfo.build(page);
    }

    @Override
    public int update(WebsiteMovieBo websiteMovie) {
        return websiteMovieMapper.updateById(websiteMovie);
    }

    private Wrapper<WebsiteMovie> buildQueryWrapper(WebsiteMovieBo websiteMovieBo) {
        Map<String, Object> params = websiteMovieBo.getParams();
        QueryWrapper<WebsiteMovie> wrapper = Wrappers.query();
        wrapper.eq("wm.del_flag", CommonConstants.NOT_DELETED)
            .eq(ObjectUtil.isNotNull(websiteMovieBo.getType()), "wm.type", websiteMovieBo.getType())
            .like(StringUtils.isNotBlank(websiteMovieBo.getTitle()), "wm.title", websiteMovieBo.getTitle())
            .eq(StringUtils.isNotBlank(websiteMovieBo.getStatus()), "wm.status", websiteMovieBo.getStatus())
            .like(ObjectUtil.isNotNull(websiteMovieBo.getCategoryId()), "wm.category_id", websiteMovieBo.getCategoryId())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "wm.create_time", params.get("beginTime"), params.get("endTime"))
            ;
        return wrapper;
    }
}
